package com.yuexun.beilunpatient.ui.docAdvice.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.docAdvice.adapter.DispensingListAdapter;
import com.yuexun.beilunpatient.ui.docAdvice.bean.DocAdvice_Dispensing;
import com.yuexun.beilunpatient.ui.docAdvice.model.impl.DispensingModel;
import com.yuexun.beilunpatient.ui.docAdvice.presenter.IDispensingPresenter;
import com.yuexun.beilunpatient.ui.docAdvice.presenter.impl.DispensingPresenter;
import com.yuexun.beilunpatient.ui.docAdvice.ui.view.IDispensingView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/29 0029.
 */

public class Frag_Dispensing extends KJFragment implements IDispensingView {
    @Bind(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    @Bind(R.id.list)
    ListView list;

    //病人住院号
    private String visitno;

    private IDispensingPresenter presenter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_docadvice, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initWidget(View parentView) {
        super.initWidget(parentView);
        presenter = new DispensingPresenter(this, new DispensingModel());
        visitno = PreferenceHelper.readString(getActivity(), AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetData();
            }
        });

        ApiGetData();
    }

    //请求数据
    private void ApiGetData() {
        if (SystemTool.checkNet(getActivity())) {
//            presenter.showDispensingList(visitno);
            presenter.showDispensingList("1801863");
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @Override
    public void showDispensingList(ArrayList<DocAdvice_Dispensing> data) {
        DispensingListAdapter adapter = new DispensingListAdapter(list, data, R.layout.item_dispensinglist);
        list.setAdapter(adapter);

        if (mEmptyLayout.isLoading()) {
            mEmptyLayout.dismiss();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
